const path = require('path')
const webpack = require("webpack")
// 页面配置
const HtmlWebpackPlugin = require('html-webpack-plugin')
// 清除文件
const {CleanWebpackPlugin} = require("clean-webpack-plugin")
// 提取css到单独文件
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
//压缩css插件
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
// 压缩发布
const ZipPlugin = require('zip-webpack-plugin');
// 读取版本号
const pkgInfo =require('./package.json')

module.exports = {
    entry: {
        login: './src/js/login.js',
        login1: './src/js/login1.js'
    },
    output: {
        filename: 'static/js/[name].js',
        path: path.resolve(__dirname, 'dist')
    },
    optimization: {
        splitChunks: {
            chunks: 'all',
            minSize: 3000,
            minChunks: 2,
            name: "vendors",
            cacheGroups: {
                jquery: {
                    test: /[\\/]node_modules[\\/](jquery)[\\/]/,
                    chunks: "initial",
                    name: "jquery",
                    minChunks: 2,
                },
                svgPanZoom: {
                    test: /[\\/]node_modules[\\/](svg-pan-zoom)[\\/]/,
                    chunks: "initial",
                    name: "svg-pan-zoom",
                    minChunks: 2,
                }
            }
        }
    },
    plugins: [
        // 登录页1
        new HtmlWebpackPlugin({
            title: '用户登录1',
            filename: 'template/login1.html',
            template: 'src/template/login1.html',
            chunks: ['jquery', 'vendors','login1']
        }),
        // 登录页
        new HtmlWebpackPlugin({
            title: '用户登录',
            filename: 'template/login.html',
            template: 'src/template/login.html',
            chunks: ['jquery', 'vendors', 'svg-pan-zoom', 'login']
        }),
        // 删除文件 保留新文件
        new CleanWebpackPlugin(),
        new MiniCssExtractPlugin({
            filename: "static/css/[name].css",
            minChunks: 1
        }),
        new OptimizeCssAssetsPlugin(),
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            'window.$':'jquery',
            'window.jQuery':'jquery'
        }),
        new ZipPlugin({
            filename: 'usas-web-'+pkgInfo.version+'.zip'
        })
    ],
    module: {
        rules: [{
            test: /\.(jpg|png|gif)$/,
            loader: 'url-loader?name=static/img/[name].[ext]&publicPath=../&limit=0'
        },{
            test: /\.css$/,
            use: [MiniCssExtractPlugin.loader, 'css-loader']
        }, {
            test: /\.(eot|ttf|svg|woff2?)$/,
            use: 'file-loader?name=static/font/[name].[ext]&publicPath=../../'
        }]
    }
};
